import { http } from '@/utils/request';
import type { ApiResponse } from '@/types';

// AI对话相关类型定义
export interface AIChatSession {
  id: number;
  user_id: number;
  question_id?: number;
  session_type: string;
  title: string;
  is_active: boolean;
  created_at: string;
  updated_at: string;
  question?: {
    id: number;
    title: string;
  };
}

export interface AIChatMessage {
  id: number;
  session_id: number;
  role: 'user' | 'assistant' | 'system';
  content: string;
  model: string;
  tokens: number;
  created_at: string;
}

export interface ChatRequest {
  message: string;
  model?: string;
  session_id?: number;
  question_id?: number;
  session_type?: string;
}

export interface ChatResponse {
  session_id: number;
  message: string;
  model: string;
  tokens: number;
}

export interface CodeCompletionRequest {
  code: string;
  language: string;
  model?: string;
  question_id: number;
  current_position: string;
}

export interface CodeCompletionResponse {
  completion: string;
  model: string;
  tokens: number;
}

// AI模型选项
export const AI_MODELS = [
  { value: 'gpt-3.5-turbo', label: 'GPT-3.5 Turbo', description: '快速响应，适合日常对话' },
  { value: 'deepseek/deepseek-chat-v3.1:free', label: 'DeepSeek Chat V3.1', description: '免费模型，平衡性能和速度' },
  { value: 'x-ai/grok-4-fast:free', label: 'Grok-4 Fast', description: '快速响应，适合代码补全' },
];

export const aiApi = {
  // 获取对话会话列表
  getChatSessions(): Promise<ApiResponse<{ sessions: AIChatSession[] }>> {
    return http.get('/v1/ai/sessions');
  },

  // 创建新的对话会话
  createChatSession(data: {
    title?: string;
    session_type?: string;
    question_id?: number;
  }): Promise<ApiResponse<{ session: AIChatSession }>> {
    return http.post('/v1/ai/sessions', data);
  },

  // 获取会话消息列表
  getChatMessages(sessionId: number): Promise<ApiResponse<{ messages: AIChatMessage[] }>> {
    return http.get(`/v1/ai/sessions/${sessionId}/messages`);
  },

  // 发送对话消息
  sendMessage(data: ChatRequest): Promise<ApiResponse<ChatResponse>> {
    return http.post('/v1/ai/chat', data);
  },

  // 代码补全
  codeCompletion(data: CodeCompletionRequest): Promise<ApiResponse<CodeCompletionResponse>> {
    return http.post('/v1/ai/code-completion', data);
  },

  // 删除对话会话
  deleteChatSession(sessionId: number): Promise<ApiResponse<void>> {
    return http.delete(`/v1/ai/sessions/${sessionId}`);
  },
};
